package io.github.uicdb.liba.api.register;

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.level.block.Block;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.util.function.Supplier;

public class LangBuilder {
    private final Multimap<String, Supplier<TranslationBean>> langMultiMap = HashMultimap.create();
    private final LazyRegistry lazyRegistry;
    private String lang;

    public LangBuilder(@NotNull LazyRegistry lazyRegistry) {
        this.lazyRegistry = lazyRegistry;
    }

    public static String toTitleCase(String word) {
        if (word == null) return "Null";
        else {
            String[] strings = word.split("_");
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < strings.length; i++) {
                stringBuilder.append(StringUtils.capitalize(strings[i])).append(i != strings.length - 1 ? " " : "");
            }
            return stringBuilder.toString();
        }
    }

    public LangBuilder addBlock(Supplier<? extends Block> key, String name) {
        return add(() -> key.get().getDescriptionId(), name);
    }

    /**
     * @param lang 语言
     * @return LangBuilder
     */
    public LangBuilder switchLang(String lang) {
        this.lang = lang;
        return this;
    }

    public LangBuilder addItem(Supplier<? extends Item> key, String name) {
        return add(() -> key.get().getDescriptionId(), name);
    }

    public LangBuilder addItemStack(Supplier<ItemStack> key, String name) {
        return add(() -> key.get().getDescriptionId(), name);
    }

    public LangBuilder addEnchantment(Supplier<? extends Enchantment> key, String name) {
        return add(() -> key.get().getDescriptionId(), name);
    }

    public LangBuilder addEffect(Supplier<? extends MobEffect> key, String name) {
        return add(() -> key.get().getDescriptionId(), name);
    }

    public LangBuilder addEntityType(Supplier<? extends EntityType<?>> key, String name) {
        return add(() -> key.get().getDescriptionId(), name);
    }

    private LangBuilder add(Supplier<String> key, String value) {
        if (lang == null) lang = lazyRegistry.defaultLang;
        langMultiMap.put(lang, () -> new TranslationBean(key.get(), value));
        return this;
    }

    public LangBuilder add(String key, String value) {
        if (lang == null) lang = lazyRegistry.defaultLang;
        langMultiMap.put(lang, () -> new TranslationBean(key, value));
        return this;
    }

    public void build() {
        lazyRegistry.langMultiMap.putAll(langMultiMap);
    }
}
